package cn.seecoder.ai.serviceImpl;

import cn.seecoder.ai.dao.*;
import cn.seecoder.ai.model.po.*;
import cn.seecoder.ai.model.vo.FileInfoVO;
import cn.seecoder.ai.model.vo.ModelVO;
import cn.seecoder.ai.service.FileInfoService;
import cn.seecoder.ai.service.ModelService;
import cn.seecoder.ai.service.SquareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DingXiaoyu
 * @date 2023/4/17 13:03
 */
@Service
public class SquareServiceImpl implements SquareService {

    @Autowired
    FileShareRepository fileShareRepository;

    @Autowired
    ModelShareRepository modelShareRepository;

    @Autowired
    FileCollectRepository fileCollectRepository;

    @Autowired
    ModelCollectRepository modelCollectRepository;

    @Autowired
    FileInfoService fileInfoService;

    @Autowired
    ModelService modelService;

    @Autowired
    UserRepository userRepository;

    @Override
    public Boolean shareFile(Integer userId, Integer fileId) {
        FileShare fileShare=FileShare.builder().userId(userId).fileId(fileId).build();
        fileShareRepository.save(fileShare);
        return true;
    }

    @Override
    public Boolean shareModel(Integer userId, Integer modelId) {
        ModelShare modelShare=ModelShare.builder().userId(userId).modelId(modelId).build();
        modelShareRepository.save(modelShare);
        return true;
    }

    @Override
    public Boolean collectFile(Integer userId, Integer fileId) {
        FileCollect fileCollect=FileCollect.builder().userId(userId).fileId(fileId).build();
        fileCollectRepository.save(fileCollect);
        return true;
    }

    @Override
    public Boolean collectModel(Integer userId, Integer modelId) {
        ModelCollect modelCollect=ModelCollect.builder().userId(userId).modelId(modelId).build();
        modelCollectRepository.save(modelCollect);
        return true;
    }

    @Override
    public List<FileInfoVO> sharedFileList(Integer userId) {
        User user=userRepository.findById(userId).get();
        List<User> users=userRepository.findByOrganization(user.getOrganization());
        List<FileInfoVO> ans=new ArrayList<>();
        for (User user1 : users) {
            List<FileShare> fileShares=fileShareRepository.findByUserId(user1.getId());
            for (FileShare fileShare : fileShares) {
                FileInfoVO fileInfoVO=fileInfoService.getFileInfo(user1.getId(), fileShare.getFileId());
                fileInfoVO.setUsername(user1.getUserName());
                ans.add(fileInfoVO);
            }
        }
        return ans;
    }

    @Override
    public List<ModelVO> sharedModelList(Integer userId) {
        User user=userRepository.findById(userId).get();
        List<User> users=userRepository.findByOrganization(user.getOrganization());
        List<ModelVO> ans=new ArrayList<>();
        for (User user1 : users) {
            List<ModelShare> modelShares=modelShareRepository.findByUserId(user1.getId());
            for (ModelShare modelShare : modelShares) {
                ModelVO modelVO=modelService.getModelVO(user1.getId(), modelShare.getModelId());
                modelVO.setUsername(user1.getUserName());
                ans.add(modelVO);
            }
        }
        return ans;
    }

    @Override
    public List<FileInfoVO> collectFileList(Integer userId) {
        List<FileCollect> fileCollects=fileCollectRepository.findByUserId(userId);
        return fileCollects
                .stream()
                .map(fileCollect->
                        fileInfoService.getFileInfo(userId,fileCollect.getFileId()))
                .collect(Collectors.toList());
    }

    @Override
    public List<ModelVO> collectModelList(Integer userId) {
        List<ModelCollect> modelCollects=modelCollectRepository.findByUserId(userId);
        return modelCollects
                .stream()
                .map(modelCollect ->
                        modelService.getModelVO(userId,modelCollect.getModelId()))
                .collect(Collectors.toList());
    }
}
